home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / bison.arc / OUTPUT.C < prev    next >
C/C++ Source or Header  |  1988-07-11  |  24KB  |  1,295 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of:  (starred ones needed only for the semantic parser)
  36.  
  37. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  38.  
  39. yytname = vector of string-names indexed by bison token number
  40.  
  41. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  42.  
  43. * yyrhs = vector of items of all rules.
  44.         This is exactly what ritems contains.
  45.  
  46. * yyprhs[r] = index in yyrhs of first item for rule r.
  47.  
  48. yyr1[r] = symbol number of symbol that rule r derives.
  49.  
  50. yyr2[r] = number of symbols composing right hand side of rule r.
  51.  
  52. * yystos[s] = the symbol number of the symbol that leads to state s.
  53.  
  54. yydefact[s] = default rule to reduce with in state s,
  55.           when yytable doesn't specify something else to do.
  56.           Zero means the default is an error.
  57.  
  58. yydefgoto[i] = default state to go to after a reduction of a rule that
  59.            generates variable ntokens + i, except when yytable
  60.            specifies something else to do.
  61.  
  62. yypact[s] = index in yytable of the portion describing state s.
  63.             The lookahed token's type is used to index that portion
  64.             to find out what to do.
  65.  
  66.         If the value in yytable is positive,
  67.         we shift the token and go to that state.
  68.  
  69.         If the value is negative, it is minus a rule number to reduce by.
  70.  
  71.         If the value is zero, the default action from yydefact[s] is used.
  72.  
  73. yypgoto[i] = the index in yytable of the portion describing 
  74.              what to do after reducing a rule that derives variable i + ntokens.
  75.              This portion is indexed by the parser state number
  76.          as of before the text for this nonterminal was read.
  77.          The value from yytable is the state to go to.
  78.  
  79. yytable = a vector filled with portions for different uses,
  80.           found via yypact and yypgoto.
  81.  
  82. yycheck = a vector indexed in parallel with yytable.
  83.       It indicates, in a roundabout way, the bounds of the
  84.       portion you are trying to examine.
  85.  
  86.       Suppose that the portion of yytable starts at index p
  87.       and the index to be examined within the portion is i.
  88.       Then if yycheck[p+i] != i, i is outside the bounds
  89.       of what is actually allocated, and the default
  90.       (from yydefact or yydefgoto) should be used.
  91.       Otherwise, yytable[p+i] should be used.
  92.  
  93. YYFINAL = the state number of the termination state.
  94. YYFLAG = most negative short int.  Used to flag ??
  95. YYNTBASE = ntokens.
  96.  
  97. */
  98.  
  99. #include <stdio.h>
  100. #include "machine.h"
  101. #include "new.h"
  102. #include "files.h"
  103. #include "gram.h"
  104. #include "state.h"
  105.  
  106. #define    MAXTABLE 32767
  107.  
  108.  
  109. extern char **tags;
  110. extern int tokensetsize;
  111. extern int final_state;
  112. extern core **state_table;
  113. extern shifts **shift_table;
  114. extern errs **err_table;
  115. extern reductions **reduction_table;
  116. extern short *accessing_symbol;
  117. extern unsigned *LA;
  118. extern short *LAruleno;
  119. extern short *lookaheads;
  120. extern char *consistent;
  121. extern short *goto_map;
  122. extern short *from_state;
  123. extern short *to_state;
  124.  
  125.  
  126. static int nvectors;
  127. static int nentries;
  128. static short **froms;
  129. static short **tos;
  130. static short *tally;
  131. static short *width;
  132. static short *actrow;
  133. static short *state_count;
  134. static short *order;
  135. static short *base;
  136. static short *pos;
  137. static short *table;
  138. static short *check;
  139. static int lowzero;
  140. static int high;
  141.  
  142.  
  143.  
  144. #define    GUARDSTR    "\n#include \"%s\"\nextern int yyerror;\n\
  145. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  146. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  147. register YYLTYPE *yylsp;\n\
  148. {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
  149.  
  150. #define    ACTSTR        "\n#include \"%s\"\nextern YYSTYPE yyval;\
  151. \nextern int yychar;\
  152. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  153. register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
  154.  
  155. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  156.  
  157.  
  158.  
  159. output_headers()
  160. {
  161.   if (semantic_parser)
  162.     fprintf(fguard, GUARDSTR, attrsfile);
  163.   fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  164. /*  if (semantic_parser)    JF moved this below
  165.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  166.   fprintf(ftable, "#include <stdio.h>\n\n"); */
  167. }
  168.  
  169. output_trailers()
  170. {
  171.   if (semantic_parser)
  172.     {
  173.       fprintf(fguard, "\n    }\n}\n");
  174.       fprintf(faction, "\n    }\n}\n");
  175.     }
  176.   else
  177.     fprintf(faction, "\n}\n");
  178. }
  179.  
  180.  
  181. output()
  182. {
  183.   int c;
  184.  
  185.   /* output_token_defines(ftable);    /* JF put out token defines FIRST */
  186.   if (!semantic_parser)        /* JF Put out other stuff */
  187.     {
  188.       rewind(fattrs);
  189.       while ((c=getc(fattrs))!=EOF)
  190.         putc(c,ftable);
  191.     }
  192.  
  193.   if (semantic_parser)
  194.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  195.   fprintf(ftable, "#include <stdio.h>\n\n");
  196.  
  197.   /* Make "const" do nothing if not in ANSI C.  */
  198.   fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  199.  
  200.   free_itemsets();
  201.   output_defines();
  202.   output_token_translations();
  203.   if (semantic_parser)
  204.     output_gram();
  205.   FREE(ritem);
  206.   if (semantic_parser)
  207.     output_stos();
  208.   output_rule_data();
  209.   output_actions();
  210.   output_parser();
  211.   output_program();
  212. }
  213.  
  214. output_token_translations()
  215. {
  216.   register int i, j;
  217. /*   register short *sp; JF unused */
  218.  
  219.   if (translations)
  220.     {
  221.       fprintf(ftable,
  222.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  223.           max_user_token_number, nsyms);
  224.     
  225.       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  226.         fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  227.       else
  228.     fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  229.     
  230.       j = 10;
  231.       for (i = 1; i <= max_user_token_number; i++)
  232.     {
  233.       putc(',', ftable);
  234.     
  235.       if (j >= 10)
  236.         {
  237.           putc('\n', ftable);
  238.           j = 1;
  239.         }
  240.       else
  241.         {
  242.           j++;
  243.         }
  244.     
  245.       fprintf(ftable, "%6d", token_translations[i]);
  246.     }
  247.     
  248.       fprintf(ftable, "\n};\n");
  249.     }
  250.   else
  251.     {
  252.       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  253.     } 
  254. }
  255.  
  256.  
  257.  
  258. output_gram()
  259. {
  260.   register int i;
  261.   register int j;
  262.   register short *sp;
  263.  
  264.   fprintf(ftable, "\nstatic const short yyprhs[] = {     0");
  265.  
  266.   j = 10;
  267.   for (i = 1; i <= nrules; i++)
  268.     {
  269.       putc(',', ftable);
  270.  
  271.       if (j >= 10)
  272.     {
  273.       putc('\n', ftable);
  274.       j = 1;
  275.     }
  276.       else
  277.     {
  278.       j++;
  279.     }
  280.  
  281.       fprintf(ftable, "%6d", rrhs[i]);
  282.     }
  283.  
  284.   fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%6d", ritem[0]);
  285.  
  286.   j = 10;
  287.   for (sp = ritem + 1; *sp; sp++)
  288.     {
  289.       putc(',', ftable);
  290.  
  291.       if (j >= 10)
  292.     {
  293.       putc('\n', ftable);
  294.       j = 1;
  295.     }
  296.       else
  297.     {
  298.       j++;
  299.     }
  300.  
  301.       if (*sp > 0)
  302.     fprintf(ftable, "%6d", *sp);
  303.       else
  304.     fprintf(ftable, "     0");
  305.     }
  306.  
  307.   fprintf(ftable, "\n};\n");
  308. }
  309.  
  310.  
  311.  
  312. output_stos()
  313. {
  314.   register int i;
  315.   register int j;
  316.  
  317.   fprintf(ftable, "\nstatic const short yystos[] = {     0");
  318.  
  319.   j = 10;
  320.   for (i = 1; i < nstates; i++)
  321.     {
  322.       putc(',', ftable);
  323.  
  324.       if (j >= 10)
  325.     {
  326.       putc('\n', ftable);
  327.       j = 1;
  328.     }
  329.       else
  330.